package com.dinetech.ejecutor;

import java.util.ArrayList;

import com.dinetech.dao.SpinnerVariosAdapter;
import com.dinetech.dao.WSEvento;
import com.dinetech.entidades.Eventos;
import com.dinetech.entidades.Personal;
import com.dinetech.entidades.Session;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class Evento_Recria extends Activity {
	Button BtnActualizar;
	TextView lblPosa;
	Spinner cbxResponsable;
	Spinner cbxLineaGenetica;
	Spinner cbxUbicacion;
	Spinner cbxGenero;
	EditText txtcant,Comentario;
	Session s = Session.getInstance();
	Personal per =Personal.getInstance();
	int idPersonal,IdLG,idGenero,IdEvento,idUbicacion;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.evento_recria);
		
		Bundle bundle = getIntent().getExtras();
		IdEvento = bundle.getInt("IdEvento");

		//Llamo al hilo que ejecuta la WebServices
		new TaskListado().execute(IdEvento);
	}
	
	private class TaskListado extends AsyncTask<Integer, Void, ArrayList<Eventos>> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			

		}

		@Override
		protected ArrayList<Eventos> doInBackground(Integer... arg0) {
			// TODO Auto-generated method stub
			//Looper.prepare();

	    	WSEvento WSEvento= new WSEvento();
			ArrayList<Eventos> lstEventos = new ArrayList<Eventos>();
			int IdEvento = Integer.parseInt(arg0[0].toString());
			lstEventos = WSEvento.WSListarEventosRegistrados(IdEvento);
	    	
	    	return lstEventos;

		}

		@Override
		protected void onPostExecute(ArrayList<Eventos> lstEventos) {
			// TODO Auto-generated method stub
			super.onPostExecute(lstEventos);
			
			idGenero = lstEventos.get(0).getCodGenero();
			BtnActualizar = (Button)findViewById(R.id.btnRecria);
			IdLG = lstEventos.get(0).getCodLineaGenetica();
			
			BtnActualizar.setText(R.string.btn_Actualizar);

			cbxUbicacion = (Spinner)findViewById(R.id.cbxPozaR);
			cbxResponsable =(Spinner)findViewById(R.id.cbxResponsableR);
			cbxLineaGenetica = (Spinner)findViewById(R.id.cbxLineaGeneticaR);
			cbxGenero =(Spinner)findViewById(R.id.cbxSexoR);
			txtcant = (EditText)findViewById(R.id.txtCantR);
			Comentario = (EditText)findViewById(R.id.txtComentarioR);
			lblPosa = (TextView)findViewById(R.id.txtFechaIE);
			
			cbxUbicacion.setEnabled(false);
			idUbicacion = lstEventos.get(0).getIdUbicacionOrigen();
			lblPosa.setText("Poza / Jaula : "+ lstEventos.get(0).getIdClientUbibcacion());
			//Lista Cbx de Responsable
			listarSpinner();
			
			BtnActualizar.setOnClickListener(new ButtonEvento());
		}

	}
	
	private void listarSpinner(){
		
		SpinnerVariosAdapter adapterR = new SpinnerVariosAdapter(Evento_Recria.this, s.getLISTA_RESPONSABLE());
		cbxResponsable.setAdapter(adapterR);
		cbxResponsable.setOnItemSelectedListener(new SpinnerListenerR());
		
		for(int i=0; i <  adapterR.getCount(); i++){
			int idRA=(int)(long)adapterR.getItemId(i) ;
			idPersonal = per.getIdPersonal();
			if( idRA == idPersonal){
				cbxResponsable.setSelection(i);					
			}	        
		}
		
		//LineaGenetica
				SpinnerVariosAdapter adapterRLineaGenetica = new SpinnerVariosAdapter(Evento_Recria.this, s.getLISTA_LINEA_GENETICA());
				cbxLineaGenetica.setAdapter(adapterRLineaGenetica);
				cbxLineaGenetica.setOnItemSelectedListener(new SpinnerListenerLGenetica());
				
				for(int i=0; i <  adapterRLineaGenetica.getCount(); i++){
					int idLGenetica=(int)(long)adapterRLineaGenetica.getItemId(i) ;
					
					if( idLGenetica == IdLG){
						cbxLineaGenetica.setSelection(i);					
					}	        
				}
		//Genero
				SpinnerVariosAdapter adapterGenero = new SpinnerVariosAdapter(Evento_Recria.this, s.getLISTA_GENERO());
				cbxGenero.setAdapter(adapterGenero);
				cbxGenero.setOnItemSelectedListener(new SpinnerListenerGenero());
				
				for(int i=0; i <  adapterGenero.getCount(); i++){
					int idRA=(int)(long)adapterGenero.getItemId(i) ;
					if( idRA == idGenero){
						cbxGenero.setSelection(i);					
					}	        
				}
		
	}
	public class SpinnerListenerR implements OnItemSelectedListener {
	      
        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        
            int convert = Integer.parseInt( Long.toString(id));
            idPersonal = convert;	            	         
        }

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			
			
		}
	}
	
	public class SpinnerListenerLGenetica implements OnItemSelectedListener {
	      
        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        
            int convert = Integer.parseInt( Long.toString(id));
            IdLG = convert;	            	         
        }

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			
			
		}
	}
	
	public class SpinnerListenerGenero implements OnItemSelectedListener {
	      
        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        
            int convert = Integer.parseInt( Long.toString(id));
            idGenero = convert;	            	         
        }

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			
			
		}
	}
	
	private class ButtonEvento implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			new TaskActualizarEvento().execute();	
		}
	
	}
	
	public class TaskActualizarEvento  extends AsyncTask<String,Void,String>{

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}
		@Override
		protected String doInBackground(String... arg0) {
			// TODO Auto-generated method stub
			String IdLG_;
			if(String.valueOf(IdLG).length()==1){
				IdLG_ = "0"+String.valueOf(IdLG);
			}else{IdLG_ = String.valueOf(IdLG);}
			
			WSEvento we = new WSEvento();
			String Msg = "";
			if(idGenero == 1){
                Msg = we.ActualizarEvento(IdEvento,"01", "06", "0", IdLG_, s.obtenerFechaActual(), String.valueOf(idUbicacion), "0", 
                              Integer.parseInt(txtcant.getText().toString()), 0, 0, "0", "0", "0", "0"+String.valueOf(idGenero), 0, Comentario.getText().toString(), 
                              idPersonal, s.obtenerFechaActual(), per.getIdPersonal(), 0, "0");
          }
          else if(idGenero == 2){
                Msg = we.ActualizarEvento(IdEvento,"01", "06", "0",IdLG_, s.obtenerFechaActual(), String.valueOf(idUbicacion), "0", 
                              0, Integer.parseInt(txtcant.getText().toString()), 0, "0", "0", "0", "0"+String.valueOf(idGenero), 0,  Comentario.getText().toString(), 
                              idPersonal, s.obtenerFechaActual(), per.getIdPersonal(), 0, "0");
          }

			return Msg;
	    		
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			Alert(result);
		}
	
	}
	
	public void Alert(String Msj){
		Toast.makeText(getApplicationContext(), Msj, Toast.LENGTH_SHORT).show();
	}
}
